#include <iostream>
#include <iomanip>
#include <unistd.h>
#include <string>
using namespace std;

void ProcessBar(const int _total = 100)
{
    const int barwidth = 40;
    const int total = _total;
    // 颜色转义码
    const string GREEN = "\033[32m";
    const string BRIGHT_GREEN = "\033[92m";
    const string RESET = "\033[0m";

    for(int i = 0; i <= total; ++i)
    {
        float process = static_cast<float>(i) / total;
        int pos = static_cast<int>(barwidth * process);

        cout << BRIGHT_GREEN;
        cout << "[";
        for(int j = 0; j < barwidth; ++j)
        {
            if(j < pos) cout << "=";
            else if(j == pos) cout << ">";
            else cout << " ";
        }
        cout << "]";

        cout << " " << setw(3) << "[" << i << "%" << "]";
        cout << RESET << "\r";
        cout.flush();
        usleep(100000);
    }
    cout << endl << "任务完成" << endl;
}

int main()
{
    ProcessBar(100);
    return 0;
}
